package com.yuwenxin.dao.impl;

import com.yuwenxin.core.BaseDaoImpl;
import com.yuwenxin.core.BaseQueryModel;
import com.yuwenxin.dao.UserDao;
import com.yuwenxin.bean.User;
import com.yuwenxin.bean.queryModel.UserQueryModel;

import java.sql.ResultSet;
import java.sql.Date;
import java.util.HashSet;

/**
 * 在此只需要构造出处理数据所需要的sql语句即可
 * 抽象的逻辑方法在BaseDaoImpl中已经实现
 */
public class UserDaoImpl extends BaseDaoImpl implements UserDao {

	@Override
	public String getSelectAllSql() {
		return "select * from user";
	}

	@Override
	public String getSelectByNameSql() {
		return "select * from user where username=?";
	}

	@Override
	public String getFuzzyFindSql() {
		return "select * from user where username like ?";
	}

	@Override
	public String getDeleteSql() {
		return "delete from user where iduser=?";
	}

	@Override
	public String getSelectByIdSql() {
		return "select * from user where iduser=?";
	}

	@Override
	//注册时调用该insert语句
	//暂时只保留username，password，email和sex属性。以此来降低注册的阈值，有利于前期用户的增长。
	public String getInsertSql(Object object) {
		User _user = (User) object;
		// 15个属性
		String username = _user.getName();
		String password = _user.getPassword();
		String email = _user.getEmail();
		String headPhotoPath = UserDao.DEFAULT_PATH + "defalt_photo.jpg";
		Date registerDate = new Date(new java.util.Date().getTime());

//		String sex = _user.getSex();
//		String phoneNum = _user.getPhoneNum();
//		String realName = _user.getRealName();
//		String idcard = _user.getIdCard();
//		String introduction = _user.getIntroduction();

		return "insert into user (username,password,email,headPhotoPath,registerDate) values ('" +
				username + "','" + password + "','" + email + "','"+ headPhotoPath + "','" + registerDate +"')";
	}

	@Override
	// 此处要采用方法重载的方式获取所需要更新的column列的信息
	// 不同的业务逻辑采取不同的传入参数进行区分
	public String getUpdateSql(Object object) throws Exception {
		/**
		 * 直接在Dao的实现层中实现对model层中可变参数的update
		 * !!!这里需要先调用findByName方法在数据库中查找到对应的对象
		 * 避免更新时将一些属性值重置
		 */
		User user_ = (User) object;
		User user = (User) findByName(user_.getName());
		//noinspection StringBufferReplaceableByString
		StringBuilder sb = new StringBuilder("update user set username='");
		sb.append(user.getName()).append("',password='").append(user.getPassword()).append("',sex='").append(user.getSex())
				.append("',introduction='").append(user.getIntroduction()).append("',isValidate=").append(user.isValidate()?1:0)
				.append(",isAdmin=").append(user.isAdmin()?1:0).append(",latestLoginDate='").append(new Date(user.getLatestLoginDate().getTime()))
				.append("' where iduser=").append(user.getId());
		return sb.toString();
	}

	@Override
	public String getConditionSql(BaseQueryModel queryModel) {  // 此函数用来判断用户登录的验证正确与否
		UserQueryModel qm = (UserQueryModel)queryModel;  // 用户通过网页客户端（html表单，jsp表单或ajax信息）传递过来的querymodel
		
		StringBuilder sb = new StringBuilder();
		sb.append("select * from users where 1=1");

		sb.append(" and id=").append(qm.getId());
		if(qm.getName()!=null && qm.getName().trim().length()>0){
			sb.append(" and name='").append(qm.getName()).append("'");
		}
		if(qm.getPassword()!=null && qm.getPassword().trim().length()>0){
			sb.append(" and password='").append(qm.getPassword()).append("'");
		}
		
		return sb.toString();
	}

	@Override
	//ResultHandler结果处理，将数据封装为一个User对象
	public User toEntity(ResultSet rs) throws Exception{
		User user = new User();
		user.setId(rs.getInt("iduser"));
		user.setName(rs.getString("username"));
		user.setPasswordDirect(rs.getString("password"));  //返回具体类时需要使用直接设置方法，避免多次加密 --9.20
		user.setSex(rs.getString("sex"));
		user.setEmail(rs.getString("email"));
		user.setHeadPhotoPath(rs.getString("headPhotoPath"));
		user.setPhoneNum(rs.getString("phoneNum"));
		user.setRealName(rs.getString("realName"));
		user.setIdCard(rs.getString("idcard"));
		user.setIntroduction(rs.getString("introduction"));
		user.setAdmin(rs.getBoolean("isAdmin"));
		user.setValidate(rs.getBoolean("isValidate"));
		user.setFriends(new HashSet<>());
		return user;
	}

}
